package com.lfy.modules.letter.listener;

import com.lfy.modules.letter.domain.SiteLetterEntity;
import com.lfy.modules.letter.domain.event.LetterEvent;
import com.lfy.modules.letter.mapper.SiteLetterMapper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

import javax.annotation.Resource;

@Component
public class LetterEventListener {
    @Resource
    private SiteLetterMapper siteLetterMapper;
    @TransactionalEventListener(classes = {LetterEvent.class},fallbackExecution = true,phase = TransactionPhase.AFTER_COMMIT)
    @Async("mainThreadPool")
    public void sendLetter(LetterEvent event) {
        SiteLetterEntity siteLetterEntity = event.getSiteLetterEntity();
        if(siteLetterEntity!= null){
            Integer isSendEmail = siteLetterEntity.getIsSendEmail();

            if(isSendEmail!= null && isSendEmail == 1){
                //TODO 发送邮件,待重构修改到字典全局配置
            }
            siteLetterMapper.insert(siteLetterEntity);
        }
    }
}
